<template>
  <div class="settings-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>系统设置</span>
        </div>
      </template>

      <el-tabs v-model="activeTab">
        <!-- 基本设置 -->
        <el-tab-pane label="基本设置" name="basic">
          <el-form
            ref="basicForm"
            :model="basicSettings"
            :rules="basicRules"
            label-width="120px"
          >
            <el-form-item label="系统名称" prop="systemName">
              <el-input v-model="basicSettings.systemName" />
            </el-form-item>

            <el-form-item label="系统Logo">
              <el-upload
                class="logo-uploader"
                action="/api/upload"
                :show-file-list="false"
                :on-success="handleLogoSuccess"
                :before-upload="beforeLogoUpload"
              >
                <img
                  v-if="basicSettings.logo"
                  :src="basicSettings.logo"
                  class="logo"
                />
                <el-icon v-else class="logo-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>

            <el-form-item label="登录页背景">
              <el-upload
                class="background-uploader"
                action="/api/upload"
                :show-file-list="false"
                :on-success="handleBackgroundSuccess"
                :before-upload="beforeBackgroundUpload"
              >
                <img
                  v-if="basicSettings.loginBackground"
                  :src="basicSettings.loginBackground"
                  class="background"
                />
                <el-icon v-else class="background-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="saveBasicSettings">
                保存设置
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <!-- 考试设置 -->
        <el-tab-pane label="考试设置" name="exam">
          <el-form
            ref="examForm"
            :model="examSettings"
            :rules="examRules"
            label-width="200px"
          >
            <el-form-item label="默认考试时长（分钟）" prop="defaultDuration">
              <el-input-number
                v-model="examSettings.defaultDuration"
                :min="30"
                :max="180"
                :step="30"
              />
            </el-form-item>

            <el-form-item label="允许切换窗口次数" prop="allowedSwitchCount">
              <el-input-number
                v-model="examSettings.allowedSwitchCount"
                :min="0"
                :max="10"
              />
              <span class="setting-hint">0表示不允许切换窗口</span>
            </el-form-item>

            <el-form-item label="自动保存答案间隔（秒）" prop="autoSaveInterval">
              <el-input-number
                v-model="examSettings.autoSaveInterval"
                :min="30"
                :max="300"
                :step="30"
              />
            </el-form-item>

            <el-form-item label="防作弊设置">
              <el-checkbox-group v-model="examSettings.antiCheating">
                <el-checkbox label="禁止复制粘贴">禁止复制粘贴</el-checkbox>
                <el-checkbox label="禁止右键菜单">禁止右键菜单</el-checkbox>
                <el-checkbox label="检测多显示器">检测多显示器</el-checkbox>
                <el-checkbox label="全屏模式">强制全屏模式</el-checkbox>
              </el-checkbox-group>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="saveExamSettings">
                保存设置
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <!-- 邮件设置 -->
        <el-tab-pane label="邮件设置" name="email">
          <el-form
            ref="emailForm"
            :model="emailSettings"
            :rules="emailRules"
            label-width="120px"
          >
            <el-form-item label="SMTP服务器" prop="smtpServer">
              <el-input v-model="emailSettings.smtpServer" />
            </el-form-item>

            <el-form-item label="SMTP端口" prop="smtpPort">
              <el-input v-model="emailSettings.smtpPort" />
            </el-form-item>

            <el-form-item label="发件邮箱" prop="senderEmail">
              <el-input v-model="emailSettings.senderEmail" />
            </el-form-item>

            <el-form-item label="邮箱密码" prop="emailPassword">
              <el-input
                v-model="emailSettings.emailPassword"
                type="password"
                show-password
              />
            </el-form-item>

            <el-form-item label="邮件通知">
              <el-checkbox-group v-model="emailSettings.notifications">
                <el-checkbox label="examCreated">考试创建通知</el-checkbox>
                <el-checkbox label="examStarting">考试开始提醒</el-checkbox>
                <el-checkbox label="gradeReleased">成绩发布通知</el-checkbox>
              </el-checkbox-group>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="saveEmailSettings">
                保存设置
              </el-button>
              <el-button @click="testEmailSettings">发送测试邮件</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const activeTab = ref('basic')

// 基本设置
const basicSettings = reactive({
  systemName: 'C程序设计考试系统',
  logo: '',
  loginBackground: ''
})

const basicRules = {
  systemName: [
    { required: true, message: '请输入系统名称', trigger: 'blur' }
  ]
}

// 考试设置
const examSettings = reactive({
  defaultDuration: 120,
  allowedSwitchCount: 3,
  autoSaveInterval: 60,
  antiCheating: ['禁止复制粘贴', '禁止右键菜单']
})

const examRules = {
  defaultDuration: [
    { required: true, message: '请设置默认考试时长', trigger: 'blur' }
  ]
}

// 邮件设置
const emailSettings = reactive({
  smtpServer: 'smtp.example.com',
  smtpPort: '465',
  senderEmail: 'noreply@example.com',
  emailPassword: '',
  notifications: ['examCreated', 'examStarting']
})

const emailRules = {
  smtpServer: [
    { required: true, message: '请输入SMTP服务器地址', trigger: 'blur' }
  ],
  smtpPort: [
    { required: true, message: '请输入SMTP端口', trigger: 'blur' }
  ],
  senderEmail: [
    { required: true, message: '请输入发件邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

// 文件上传处理
const handleLogoSuccess = (response) => {
  basicSettings.logo = response.url
}

const beforeLogoUpload = (file) => {
  const isImage = file.type.startsWith('image/')
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isImage) {
    ElMessage.error('只能上传图片文件!')
    return false
  }
  if (!isLt2M) {
    ElMessage.error('图片大小不能超过 2MB!')
    return false
  }
  return true
}

const handleBackgroundSuccess = (response) => {
  basicSettings.loginBackground = response.url
}

const beforeBackgroundUpload = (file) => {
  const isImage = file.type.startsWith('image/')
  const isLt5M = file.size / 1024 / 1024 < 5

  if (!isImage) {
    ElMessage.error('只能上传图片文件!')
    return false
  }
  if (!isLt5M) {
    ElMessage.error('图片大小不能超过 5MB!')
    return false
  }
  return true
}

// 保存设置
const saveBasicSettings = () => {
  ElMessage.success('基本设置保存成功')
}

const saveExamSettings = () => {
  ElMessage.success('考试设置保存成功')
}

const saveEmailSettings = () => {
  ElMessage.success('邮件设置保存成功')
}

// 其他操作
const testEmailSettings = () => {
  ElMessage.success('测试邮件发送成功')
}
</script>

<style scoped>
.settings-container {
  padding: 20px;
}

.setting-hint {
  margin-left: 10px;
  color: #909399;
}

.logo-uploader,
.background-uploader {
  text-align: center;
}

.logo {
  width: 100px;
  height: 100px;
  object-fit: contain;
}

.background {
  width: 200px;
  height: 112px;
  object-fit: cover;
}

.logo-uploader .el-upload,
.background-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.logo-uploader .el-upload {
  border-radius: 50%;
  width: 100px;
  height: 100px;
  line-height: 100px;
}

.background-uploader .el-upload {
  border-radius: 4px;
  width: 200px;
  height: 112px;
  line-height: 112px;
}

.logo-uploader .el-upload:hover,
.background-uploader .el-upload:hover {
  border-color: #409EFF;
}

.logo-uploader-icon,
.background-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100%;
  height: 100%;
  text-align: center;
}
</style> 